home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / gold / mboxMethods.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.8 KB  |  221 lines

  1. /*
  2.  * The original copyright owners of the accompanying source code files have
  3.  * agreed to place such code into the public domain.  Accordingly, anyone
  4.  * who receives or obtains a copy of such source code is freely entitled to
  5.  * reproduce, use and otherwise exploit such code (including the right to
  6.  * make derivative works), at his/her own risk and expense, without any
  7.  * obligation or liability to the original copyright owners.
  8.  *
  9.  * We would appreciate (but do not require) that the following message be
  10.  * included in any derivative works:
  11.  *
  12.  * "Portions of this program were developed by Peter Broadwell, Rob Myers
  13.  * and Robin Schaufler while working in Silicon Valley."
  14.  *
  15.  * The accompanying source code files and related documentation materials
  16.  * are distributed on an "AS IS" basis, without any warranties or
  17.  * guarantees of any kind.  All implied warranties, including the implied
  18.  * warranties of merchantability and of fitness for any particular purpose,
  19.  * are expressly disclaimed.
  20.  */
  21. #include <stdio.h>
  22. #include <math.h>
  23. #include <gl.h>
  24. #include <device.h>
  25.  
  26. #include "geom.h"
  27. #include "selectors.h"
  28. #include "class.h"
  29. #include "classIds.h"
  30. #include "mbox.h"
  31. #include "behavior.h"
  32.  
  33. extern int maldbug;
  34. extern int dumper;
  35. #ifdef MALDEBUG
  36. #define Free(a)        if(maldbug) printf("gffree(0x%x)\n",a); gffree(a)
  37. #else
  38. #define Free(a)        gffree(a)
  39. #endif /* MALDEBUG */
  40.  
  41.  
  42.     /*
  43.      *  add subscriber to a mailbox
  44.      */
  45. subscribe(self, subscriber)
  46.     register mailbox *self;
  47.     mailbox *subscriber;
  48. {
  49.     register behavior *bself = (behavior *)self;
  50.     register subscr *myNewEntry, *hisNewEntry;
  51.  
  52. /*if(maldbug) printf("subscribe(0x%x, 0x%x)\n",self,subscriber);*/
  53.  
  54.     myNewEntry  = (subscr *)gfmalloc(sizeof(subscr));
  55.     if(!myNewEntry) {
  56.     fprintf(stderr,"subscribe: my gfmalloc bombed out\n");
  57.     exit(1);
  58.     }
  59.         /*
  60.          * if subscribing to a behavior, get instance vars-sized
  61.          * subscr entry
  62.          */
  63.     if (isSuper(self, BEHAVIOR) && bself->vartemplate) {
  64.     hisNewEntry = (subscr *)gfmalloc(bself->varsize);
  65.     if(!hisNewEntry) {
  66.         fprintf(stderr,"subscribe: his vartemplate  gfmalloc bombed out\n");
  67.         exit(1);
  68.     }
  69.         /* initialize instance variables from template */
  70.     bcopy(bself->vartemplate, hisNewEntry, bself->varsize);
  71.     }
  72.     else {
  73.     hisNewEntry = (subscr *)gfmalloc(sizeof(subscr));
  74.     if(!hisNewEntry) {
  75.         fprintf(stderr,"subscribe: his gfmalloc bombed out\n");
  76.         exit(1);
  77.     }
  78.     }
  79.  
  80.     myNewEntry->next   = self->subscribers;
  81.     myNewEntry->member = (inst *)subscriber;
  82.     self->subscribers  = myNewEntry;
  83.  
  84.     hisNewEntry->next        = subscriber->subscribedTo;
  85.     hisNewEntry->member      = (inst *)self;
  86.     subscriber->subscribedTo = hisNewEntry;
  87. }
  88.  
  89.     /*
  90.      *  remove subscriber from a mailbox
  91.      */
  92. unsubscribe(self, unsubscriber)
  93.     register mailbox *self;
  94.     mailbox *unsubscriber;
  95. {
  96.     subscr *unLinkMember();
  97.  
  98. /*if(maldbug) printf("unsubscribe(0x%x, 0x%x)\n",self,unsubscriber);*/
  99.  
  100.     self->subscribers = unLinkMember(self->subscribers, unsubscriber);
  101. dumpSubscribers("FPost",self->subscribers,unsubscriber);
  102.     unsubscriber->subscribedTo =
  103.     unLinkMember(unsubscriber->subscribedTo, self);
  104. dumpSubscribers("Post",unsubscriber->subscribedTo,self);
  105. }
  106.  
  107.     /*
  108.      *  unlink a subscription entry from a subscription list
  109.      */
  110.     subscr *
  111. unLinkMember(list, member)
  112.     subscr *list;
  113.     mailbox *member;
  114. {
  115.     register subscr *entry, *nentry;
  116.  
  117.     if(! list || ! member)
  118.     return list;
  119. dumpSubscribers("pre",list,member);
  120.     if(list->member == (inst *)member) {    /* first thing in list */
  121.     nentry = list->next;
  122.     gffree(list);
  123. dumpSubscribers("fpost",nentry,member);
  124.     return nentry;
  125.     }
  126.     for (entry = list; entry; entry = entry->next) {
  127.     if (nentry = entry->next) {
  128.         if (nentry->member == (inst *)member) {
  129.         entry->next = nentry->next;
  130.         gffree(nentry);
  131.         }
  132.     }
  133.     }
  134. dumpSubscribers("post",list,member);
  135.     return list;
  136. }
  137.  
  138. #ifdef NOTDEF
  139.     /*
  140.      *  gffree entry.  other implementations may gffree pointers from entry.
  141.      */
  142. freevars(self, argtype, entry)
  143.     mailbox *self;
  144.     int argtype;
  145.     subscr *entry;
  146. {
  147.     Free(entry);
  148. }
  149. #endif /* NOTDEF */
  150.  
  151.     /*
  152.      *  find the instance variable block for class(catagory) within a subscriber
  153.      */
  154.     subscr *
  155. findvars(self, catagory)
  156.     register mailbox *self;
  157.     int catagory;
  158. {
  159.     register subscr *entry;
  160.     inst *member;
  161.  
  162.     for (entry = self->subscribedTo; entry; entry = entry->next) {
  163.     if ((member = entry->member) && isSuper(member, catagory))
  164.         return entry;
  165.     }
  166.     return NULL;
  167. }
  168.  
  169.     /*
  170.      *  halfway add subscriber to a mailbox, for future use.
  171.      *  used by eggs to remember to subscribe their offspring later.
  172.      */
  173. halfsubscr(self, subscriber)
  174.     register mailbox *self;
  175.     mailbox *subscriber;
  176. {
  177.     register behavior *bself = (behavior *)self;
  178.     register subscr *hisNewEntry;
  179.  
  180.         /*
  181.          * if subscribing to a behavior, get instance vars-sized
  182.          * subscr entry
  183.          */
  184.     if (isSuper(self, BEHAVIOR) && bself->vartemplate) {
  185.     hisNewEntry = (subscr *)gfmalloc(bself->varsize);
  186.         /* initialize instance variables from template */
  187.     bcopy(bself->vartemplate, hisNewEntry, bself->varsize);
  188.     }
  189.     else {
  190.     hisNewEntry = (subscr *)gfmalloc(sizeof(subscr));
  191.     }
  192.  
  193.     hisNewEntry->next        = subscriber->subscribedTo;
  194.     hisNewEntry->member      = (inst *)self;
  195.     subscriber->subscribedTo = hisNewEntry;
  196.  
  197. }
  198.  
  199.     /*
  200.      *  dump a subscription list
  201.      */
  202. dumpSubscribers(string, slist, wanted)
  203.     char *string;
  204.     register subscr *slist;
  205.     inst *wanted;
  206. {
  207.     register subscr *s;
  208.  
  209.     if(!dumper) return;
  210.  
  211.     printf("dumpSubscribers: %s want 0x%x\n",string,wanted);
  212.     for (s = slist; s; s = s->next) {
  213.     printf("\t 0x%x subscr->member = 0x%x",s,s->member);
  214.     if (s->member) 
  215.         printf(", class %d", s->member->myClass->classId);
  216.     if (s->member == wanted) 
  217.         printf("\t<<<<<<");
  218.     printf("\n");
  219.     }
  220. }
  221.